package com.sgb.kjwl.view.ui.fragment.transport;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentDeviceeAndMachineBinding;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.ui.activity.home.SearchActivity4;
import com.sgb.kjwl.view.ui.fragment.device.DeviceListFragment3;
import com.sgb.kjwl.view.ui.fragment.machine.MachineListFragment3;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
@Deprecated
public class MachineDeviceFragment extends AppFragment {

    private FragmentDeviceeAndMachineBinding binding;
    private ArrayList<Fragment> mFragments;
    String[] titles = {"设备", "机械"};
    private int currentChildTab = 0;

    public static MachineDeviceFragment newInstance() {
        MachineDeviceFragment fragment = new MachineDeviceFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_devicee_and_machine, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    private void initData() {
        if (mFragments == null) {
            mFragments = new ArrayList<>();
        }
        mFragments.add(DeviceListFragment3.newInstance());
        mFragments.add(MachineListFragment3.newInstance("1", null));
        SlidingFragmentPagerAdapter mAdapetr = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, titles);
        binding.viewPager.setAdapter(mAdapetr);
        binding.viewPager.setOffscreenPageLimit(2);
        binding.tablayout.setViewPager(binding.viewPager, titles);
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                currentChildTab = i;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.SEARCH_DEVICE_MACHINE) {
            String searchText = (String) event.getMessageData();
            if (this.getActivity() instanceof SearchActivity4) {
                EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag((currentChildTab == 0) ? EventConstant.SEARCH__DEVICE : EventConstant.SEARCH_MACHINE).setMessageData(searchText));
            }
            EventBus.getDefault().removeStickyEvent(event);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
